/**
 * 
 */
package me.dbest.javase.prodNcons;

/**
 * @author nishant
 * 
 */
public class Q {
    int n;
    boolean valueSet = false;

    synchronized int get() {
	if (!valueSet)
	    try {
		System.out.println("Gotcha");
		wait();
		System.out.println("Gotcha2");
	    } catch (InterruptedException e) {
		System.out.println("InterruptedException caught");
	    }
	System.out.println("Got: " + n);
	valueSet = false;
	System.out.println("Notcha");
	notify();
	System.out.println("Notcha2");
	return n;
    }

    synchronized void put(int n) {
	if (valueSet)
	    try {
		wait();
	    } catch (InterruptedException e) {
		System.out.println("InterruptedException caught");
	    }
	this.n = n;
	valueSet = true;
	System.out.println("Put: " + n);
	notify();
    }
}
